﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FA_Model;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Configuration;

namespace FA_Server
{
   public class FA_S_MasterDataCard
    {
      public FA_Model.MasterDataCard.FA_M_MasterDataCard FA_MDC;
       private List<FA_Model.MasterDataCard.FA_M_MasterDataCard> _lmdcs;
       OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString);
       OracleCommand cmd;
       private DataTable _dt;
       Dictionary<string, string> dcn;
       public FA_S_MasterDataCard(string scorpcpde, string smasterdatacard)
       {
           DataTable dt = getmdc(scorpcpde, smasterdatacard);

           FA_MDC = new FA_Model.MasterDataCard.FA_M_MasterDataCard(scorpcpde);
           FA_MDC.sMasterDataCard = smasterdatacard;
       }
       public FA_S_MasterDataCard(string scorpcode)
       {
           _dt = getmdc(scorpcode);

           dcn = getcodename(_dt.Columns, scorpcode);
           _lmdcs = new List<FA_Model.MasterDataCard.FA_M_MasterDataCard>();
           //for (int i = 0; i < _dt.Columns.Count; i++)
           //{
           //    if (dcn.ContainsKey(_dt.Columns[i].ColumnName))
           //        _dt.Columns[i].ColumnName = dcn[_dt.Columns[i].ColumnName];
           //}
           _lmdcs = GetlMDCS(_dt, scorpcode);
       }

       private List<FA_Model.MasterDataCard.FA_M_MasterDataCard> GetlMDCS(DataTable dt,string scorpcode)
       {
           for (int j = 0; j < dt.Rows.Count; j++)
           {
               FA_Model.MasterDataCard.FA_M_MasterDataCard _mdc = new FA_Model.MasterDataCard.FA_M_MasterDataCard(scorpcode);
               _mdc.sMasterDataCard = dt.Rows[j]["SMASTERCARDCODE"].ToString();
               _mdc.sAssetsCode = dt.Rows[j]["SASSETSCODE"].ToString();
               _mdc.sAssetsName = dt.Rows[j]["SASSETSNAME"].ToString();
               _mdc.sCorpName = dt.Rows[j]["SCORPNAME"].ToString();
               _mdc.D_Code_Name = dcn;
               Dictionary<string, string> dcv=new Dictionary<string,string> ();
               for (int i = 0; i < dt.Columns.Count; i++)
               {
                   if (dcn.ContainsKey(dt.Columns[i].ToString()))
                       dcv.Add(dt.Columns[i].ToString(), dt.Rows[j][i].ToString());
               }
               _mdc.D_Code_Value = dcv;
               this.lMDCS.Add(_mdc);
           }
           return lMDCS;
       }


       private DataTable getmdc(string scorpcpde, string smasterdatacard)
       {
           DataTable dtc = new DataTable();
           cmd = conn.CreateCommand();
           try
           {
               if (conn.State != ConnectionState.Open)
                   conn.Open();
               cmd.CommandText = " select * from FA_MASTERDATA where scorpcode='" + scorpcpde + "' and smastercardcode ='" + smasterdatacard + "'";
              
               using (OracleDataAdapter da = new OracleDataAdapter(cmd))
               {
                   DataSet ds = new DataSet();
                   da.Fill(ds);
                   dtc = ds.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return dtc;
       }

       private DataTable getmdc(string scorpcpde)
       {
           DataTable dtc = new DataTable();
           cmd = conn.CreateCommand();
           try
           {
               if (conn.State != ConnectionState.Open)
                   conn.Open();
               cmd.CommandText = " select A.*,C.SCORPNAME from FA_MASTERDATA A,FA_CORP C WHERE A.SCORPCODE=C.SCORPCODE AND A.SCORPCODE='" + scorpcpde + "' ORDER BY A.SMASTERCARDCODE";
               
               using (OracleDataAdapter da = new OracleDataAdapter(cmd))
               {
                   DataSet ds = new DataSet();
                   da.Fill(ds);
                   dtc = ds.Tables[0];
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return dtc;
       }

       private Dictionary<string, string> getcodename(DataColumnCollection dcol,string scorpcode)
       {
           Dictionary<string, string> rdic = new Dictionary<string, string>();
           try
           {
               if (conn.State != ConnectionState.Open)
                   conn.Open();
               cmd.CommandText = " select SCODE,SNAME from FA_PROPERTY WHERE SCORPCODE='" + scorpcode + "'ORDER BY IRANK";
               DataTable dtc = new DataTable();
               using (OracleDataAdapter da = new OracleDataAdapter(cmd))
               {
                   DataSet ds = new DataSet();
                   da.Fill(ds);
                   dtc = ds.Tables[0];
               }

               for (int i = 0; i < dtc.Rows.Count; i++)
               {
                   int j = -1;
                   if (dcol.Contains(dtc.Rows[i]["scode"].ToString()))
                   rdic.Add(dtc.Rows[i]["scode"].ToString(), dtc.Rows[i]["sname"].ToString());
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return rdic;
       }
       private Dictionary<string, string> getcodevalue(string smasterdatacard, DataRow dr)
       {
           Dictionary<string, string> rdic = new Dictionary<string, string>();
           try
           {
               for (int i = 0; i < dr.Table.Columns.Count; i++)
               {
                   rdic.Add(dr.Table.Columns[i].ColumnName,dr[i].ToString());
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return rdic;
       }

       public List<FA_Model.MasterDataCard.FA_M_MasterDataCard> lMDCS { get { return _lmdcs; } }

       public DataTable TMDC { get { return _dt; } }
    }
}
